package com.jt.aop;

import com.jt.anno.Pri;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.util.pattern.PathPattern;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Component
@Aspect // 标识为一个切面（需要导包）
@Order(1)
public class SpringAOP {
    private List<String> list = new ArrayList<>(); // 单机版的，因为ArrayList线程不安全

    @PostConstruct // 在初始化是调用方法
    private void add(){
        list.add("add");
        list.add("del");
    }
//    @Pointcut("@annotation(Pri)")
//    public void pointcut(){
//
//    }
    @Around("@annotation(pri)")
    public void around1(ProceedingJoinPoint joinPoint, Pri pri){
        String name = pri.name();
        if (list.contains(name)){
            try {
                Object proceed = joinPoint.proceed();
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }else System.out.println("您没有权限");

    }
    // @Around("execution(* com.jt.service.UserService.*(..))")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        long startTime = System.currentTimeMillis();
        Class<?> Class = joinPoint.getTarget().getClass();// 获取类型
        Object[] args = joinPoint.getArgs();// 获取参数数组
        String name = joinPoint.getSignature().getName();// 获取方法名字
        Object proceed = joinPoint.proceed();// 返回值
        long endTime = System.currentTimeMillis();
        System.out.println(endTime-startTime);
        System.out.println(name);
        System.out.println(Arrays.toString(args));
        System.out.println(Class);
    }
}
